<%= stylesheet_link_tag "/javascripts/extjs/resources/css/ext-all.css" %>
<%= stylesheet_link_tag "/javascripts/extjs/examples/ux/css/Spinner.css" %>
<%= stylesheet_link_tag "/stylesheets/images.css" %>
<%= stylesheet_link_tag "/stylesheets/ext_patch.css" %>

<%= javascript_include_tag "extjs/adapter/ext/ext-base-debug.js" %>
<%= javascript_include_tag "extjs/ext-all-debug.js" %>
<%= javascript_include_tag "application.js" %>
<%= javascript_include_tag "extjs/examples/ux/Spinner.js" %>
<%= javascript_include_tag "extjs/examples/ux/SpinnerField.js" %>
<%= javascript_include_tag "time.js" %>
<%= javascript_include_tag "extjs/src/locale/ext-lang-zh_CN.js" %>
<%= javascript_include_tag "extjs/examples/ux/RowEditor.js" %>
<%= javascript_include_tag "extjs/examples/grid/gen-names.js" %>
<%= javascript_include_tag "Datetime.js" %>

<script type="text/javascript">
  Ext.QuickTips.init();
  Ext.lib.Ajax.defaultPostHeader += ";charset=UTF-8";
  Ext.Msg.minWidth = 450;
  Ext.BLANK_IMAGE_URL = "/javascripts/extjs/resources/images/default/s.gif";

  Ext.apply(Ext.form.VTypes, {
    daterange : function(val, field) {
      var date = field.parseDate(val);

      if(!date){
        return;
      }
      if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
        var start = Ext.getCmp(field.startDateField);
        start.setMaxValue(date);
        start.validate();
        this.dateRangeMax = date;
      }
      else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
        var end = Ext.getCmp(field.endDateField);
        end.setMinValue(date);
        end.validate();
        this.dateRangeMin = date;
      }
      /*
       * Always return true since we're only using this vtype to set the
       * min/max allowed values (these are tested for after the vtype test)
       */
      return true;
    },

    password : function(val, field) {
      if (field.initialPassField) {
        var pwd = Ext.getCmp(field.initialPassField);
        return (val == pwd.getValue());
      }
      return true;
    },

    passwordText : 'Passwords do not match'
  });

  function logout(){
    Ext.Ajax.request({
      url : '/index/logout',
      success : function (result, request) {
        window.document.location.href = '/index/login';
      },
      failure : function (result, request){
        Ext.MessageBox.alert("提示","通讯失败，请从新操作");
      },
      method : 'POST'
    });
  }

  Ext.onReady(function() {

    var topPanel = new Ext.Panel({
      frame:false,
      height:0,
      tbar: ['<b style="font-size:14px;"><%=session[:user_name]%>，您好。欢迎使用济南局动车组日常检修系统', '->', {
          text: '注销',
          iconCls:'logout',
          id:'exit',
          scope: this,
          handler: function() {
            Ext.getCmp('exit').setDisabled(true);
            Ext.Ajax.request({
              url : '/index/logout',
              success : function (result, request) {
                window.document.location.href = '/index/login';
              },
              failure : function (result, request){
                Ext.MessageBox.alert("提示","通讯失败，请从新操作");
                Ext.getCmp('exit').setDisabled(false);
              },
              method : 'POST'
            });
          }
        }],
      cls:'empty',
      region:'north'
    });

    //左边的菜单栏
    var accordition = new Ext.Panel({
      region:'west',
      margins:'5 0 5 5',
      split:true,
      width: 140,
      layout:'accordion'
    });

        

    //工作区
    var workTabs = new Ext.TabPanel({
      //renderTo:'tabs',
      region:'center',
      activeTab: 0,
      resizeTabs:true, // turn on tab resizing
      minTabWidth: 115,
      tabWidth:135,
      enableTabScroll:true,
      //width:600,
      //height:250,
      defaults: {autoScroll:true}
    });

    var reminderPanel = new Ext.Panel({
      title: '系统提醒',
      id:'reminder_panel',
      //html: '&lt;empty panel&gt;',
      //cls:'cellpadding:10',
      defaults:{margins:'5 5 5 5'},
      //bodyStyle: 'padding:15px;align:center',
      html:'<center><br><br><br><br></center>',
      listeners: {
        //expand: showAgileDiagram
      }
    });
    workTabs.add(reminderPanel).show();

    workTabs.openTab = function(node) {
      // var id = node.attributes.url;
      var id = node.attributes.url;
      var url = node.attributes.url;
      var tabTitle = node.text;
      var titleStyle = ['background:url(\'', node.ui.getIconEl().src, '\') no-repeat; padding-left:20px'].join('');
          
      workTabs.getItem(0).setTitle(tabTitle);
      workTabs.getItem(0).load({
        url: url,
        scripts: true
      });
      //workTabs.getItem(0).getUpdater().update(url);
      workTabs.getItem(0).show();
    };
        
    var viewport = new Ext.Viewport({
      layout:'border',
      items:[topPanel,accordition, workTabs]
    });

    //添加详细菜单
    function renderMenu(){
      var menus = eval(<%=raw @menus%>);
          
      if (!menus) { return; }
      for (var i = 0; i < menus.length; i++) {
        var item = menus[i];

        var title = "<div style='background:url(" + item.image +
          ") no-repeat;padding-left:20px;'>" +
          item.name +
          "</div>";
        for (var j = 0; j < item.children.length; j++) {
          var o = item.children[j];
          o.text = o.name;
          o.icon = o.image;
        }
        var node = new Ext.tree.TreePanel({
          title: title,
          rootVisible: false,
          lines: false,
          autoScroll: true,
          qtips: item.qitps,
          root: {
            editable: false,
            expanded: true,
            text: item.name,
            draggable: false,
            children: item.children
          },
          listeners: {
            click: function(node, e) {
              workTabs.openTab(node);
            }
          }
        });
        accordition.add(node);
      }
      accordition.doLayout();
    }
    renderMenu();
  });
</script>
